package com.systemCapacity.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.systemCapacity.common.dto.ActivityPerformanceRequestDto;
import com.systemCapacity.entity.ActivityPerformanceRequest;
import com.systemCapacity.entity.Attribute;
import com.systemCapacity.entity.Enum.AttributeValueType;
import com.systemCapacity.mapper.ActivityPerformanceMapper;
import com.systemCapacity.service.ActivityPerformanceService;
import com.systemCapacity.service.AttributeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class ActivityPerformanceServiceImpl extends ServiceImpl<ActivityPerformanceMapper, ActivityPerformanceRequest>
        implements ActivityPerformanceService {

    @Autowired
    ActivityPerformanceMapper activityPerformanceMapper;

    @Autowired
    AttributeService attributeService;

    @Override
    public List<ActivityPerformanceRequestDto> getPerformances(String requestId) {
        QueryWrapper<ActivityPerformanceRequest> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", requestId);
        List<ActivityPerformanceRequest> requests = activityPerformanceMapper.selectList(queryWrapper);
        return requests.stream().map(
                request -> {
                    ActivityPerformanceRequestDto dto = BeanUtil.copyProperties(request, ActivityPerformanceRequestDto.class);
                    List<Integer> values = Arrays.stream(request.getRequestValues().split(";"))
                            .map(Integer::parseInt).collect(Collectors.toList());
                    Attribute attribute = attributeService.getById(dto.getAttributeId());
                    dto.setAttributeName(attribute.getAttributeName());
                    dto.setUnit(attribute.getUnit());
                    dto.setAttrValues(values);
                    // 判断是否满足要求
                    judgePerformanceSatisfied(dto, values, attribute);
                    return dto;
                }
        ).collect(Collectors.toList());
    }

    private void judgePerformanceSatisfied(ActivityPerformanceRequestDto dto, List<Integer> values, Attribute attribute) {
        String equipmentValue = attribute.getValue();
        if (attribute.getAttributeValueType() == AttributeValueType.NUMERIC) {
            int intValue = Integer.parseInt(equipmentValue);
            switch (dto.getRelation()) {
                case LESS:
                    dto.setSatisfied(intValue <= values.get(0));
                    break;
                case MORE:
                    dto.setSatisfied(intValue >= values.get(0));
                    break;
                default:
                    dto.setSatisfied(intValue >= values.get(0) && intValue <= values.get(1));
            }
        } else {
            String[] rangeLimit = equipmentValue.split(";");
            switch (dto.getRelation()) {
                case LESS:
                    dto.setSatisfied(Integer.parseInt(rangeLimit[0]) <= values.get(0));
                    break;
                case MORE:
                    dto.setSatisfied(Integer.parseInt(rangeLimit[1]) >= values.get(0));
                    break;
                default:
                    // 装备参数区间包括要求区间
                    dto.setSatisfied(Integer.parseInt(rangeLimit[0]) <= values.get(0)
                            && Integer.parseInt(rangeLimit[1]) >= values.get(1));
            }
        }
    }

    @Override
    @Transactional
    public boolean savePerformances(ActivityPerformanceRequestDto dto, String requestId) {
        ActivityPerformanceRequest request = new ActivityPerformanceRequest();
        request.setRequestId(UUID.randomUUID().toString());
        request.setPid(requestId);
        request.setRelation(dto.getRelation());
        request.setAttributeId(dto.getAttributeId());
        List<Integer> values = dto.getAttrValues();
        StringBuilder stringBuilder = new StringBuilder();
        values.forEach(value -> stringBuilder.append(value).append(";"));
        stringBuilder.setLength(Math.max(stringBuilder.length() - 1, 0));
        request.setRequestValues(stringBuilder.toString());
        save(request);
        return false;
    }

    @Override
    public boolean deletePerformanceByPid(String pid) {
        QueryWrapper<ActivityPerformanceRequest> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", pid);
        return remove(queryWrapper);
    }
}
